package com.car.controller;


import com.car.entity.*;
import com.car.service.ICarService;
import com.car.service.IVipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class VipController {
    @Autowired
    @Qualifier("vipService")
    public IVipService iVipService;

    @Autowired
    @Qualifier("carService")
    public ICarService iCarService;

    //添加会员
    @RequestMapping(value = "/addVip")
    public String addVip(Vip vip, Car car){
        vip.setRegtime(new Date());
        iVipService.addVip(vip);
        car.setVid(9);
        iVipService.addCar1(car);
        return "redirect:findVipAll";
    }

    //查询本店会员
    @GetMapping( value = "/findVipAll")
    public String findAll(Model model, HttpSession session){
        List<HuiYuan> vips = iVipService.findAll();
        session.setAttribute("vips",vips);
        model.addAttribute("vips",vips);
        return "/user_List.jsp";
    }

    //根据vid查询订单
    @RequestMapping (value = "findB",method = RequestMethod.POST)
    public String findB(int vid, Model model,HttpSession session){
        List<Billing> b = iVipService.findB(vid);
        model.addAttribute("list1",b);
        session.setAttribute("list1",b);
        return "/order_List.jsp";
    }
    //查看详情信息
    @RequestMapping(value = "findXQ",method = RequestMethod.GET)
    public String findXQ(int vid,HttpSession session,Model model){
        List<Billing> b = iVipService.findB(vid);
        List<Car> car = iVipService.findCar(vid);
        List<Pet_card> pet = iVipService.findPet(vid);
        List<Year_card> year = iVipService.findYear(vid);
        List<Gift> gift = iVipService.findGift(vid);
        model.addAttribute("gift",gift);
        model.addAttribute("year",year);
        model.addAttribute("pet",pet);
        session.setAttribute("b",b);
        model.addAttribute("b",b);
        session.setAttribute("car",car);
        return "user_Detail.jsp";
    }
    //查看大礼包详情
    @RequestMapping(value = "findLB",method = RequestMethod.GET)
    public String FindLBB(int wid,Model model){
        System.out.println("wid"+wid);
        List<Details> details = iVipService.findDetails(wid);

        model.addAttribute("details",details);
        return "user_Package.jsp";
    }

    //会员信息的查询
    @RequestMapping(value = "findIDR",method = RequestMethod.GET)
    public String FindIDR(String vname,Model model,HttpSession session){
        System.out.println("find");
        if (vname!=null){
            Vip idr1 = iVipService.findIDR1(vname);
            int vid = idr1.getVid();
            List<Car> all = iCarService.findAll();
            List<Pet_card> pet = iVipService.findPet(vid);
            List<VipCarYear> year1 = iVipService.findYear1(vid);
            List<GiftVip> gift1 = iVipService.findGift1(vid);
            List<CalendarVip> calendarVip = iVipService.findCalendarVip(vid);
            List<IntegralVip> integralVip = iVipService.findIntegralVip(vid);
            List<Car> car = iVipService.findCar(vid);
            List<Emp> emp = iVipService.findEmp();
            model.addAttribute("idr1", idr1);
            model.addAttribute("pet", pet);
            model.addAttribute("year1", year1);
            model.addAttribute("gift1", gift1);
            model.addAttribute("calendarVip", calendarVip);
            model.addAttribute("integralVip", integralVip);
            model.addAttribute("car", car);
            session.setAttribute("idr1", idr1);
            session.setAttribute("emp", emp);;
            session.setAttribute("pet", pet);
            session.setAttribute("all1", all);
            session.setAttribute("car", car);
            session.setAttribute("year1", year1);
            session.setAttribute("gift1", gift1);
            session.setAttribute("calendarVip", calendarVip);
            session.setAttribute("integralVip", integralVip);
            return "redirect:user_Info.jsp";
        }else {
            return "redirect:user_Info.jsp";
        }
    }
    //修改会员信息
    @RequestMapping(value = "/updateVip")
    public String updateVip(Vip vip){
        iVipService.updateVip(vip);
        return "redirect:findIDR";
    }

    //添加爱车
    @RequestMapping(value = "/addCar")
    public String addCar(Car car){
        iVipService.addCar(car);
        return "redirect:findIDR";
    }
    //通过vid查询车
    @RequestMapping(value = "/findIdCar" ,method = RequestMethod.GET)
    public String findIdCar(int cid,Model model){
        Car idCar = iVipService.findIdCar(cid);
        model.addAttribute("idCar",idCar);
        return "edit_Car.jsp";
    }
    //通过pid查询存储卡
    @RequestMapping(value = "/findIdPet_card" ,method = RequestMethod.GET)
    public String findIdPet_card(int pid,Model model){
        Pet_card idPet_card = iVipService.findIdPet_card(pid);
        model.addAttribute("idPet_card",idPet_card);
        return "pcard_Recharge.jsp";
    }
    //卡片充值
    @RequestMapping(value = "/updatePet_card" ,method = RequestMethod.GET)
    public String updatePet_card(Pet_card pet_card){
        System.out.println("12323"+pet_card);
        iVipService.updatePet_card(pet_card);
        return "redirect:findIDR";
    }

    //修改车的资料
    @RequestMapping(value = "/updateCar" ,method = RequestMethod.GET)
    public String updateCar(Car car){
        iVipService.updateCar(car);
        return "redirect:findIDR";
    }

    //删除车的信息
    @RequestMapping(value = "deleteCar")
    public String deleteCar(int cid){
        iVipService.deleteCar(cid);
        return "redirect:findIDR";
    }

    //删除卡片
    @RequestMapping(value = "kxq",method = RequestMethod.GET)
    public String kpdelete(int pid){
        System.out.println("pid"+pid);
        iVipService.kpdelete(pid);
        return "redirect:findIDR";
    }
    //添加储存卡
    @RequestMapping(value = "/addpet")
    public String addpet(Pet_card pet_card){
        iVipService.addPet_card(pet_card);
        return "redirect:findIDR";
    }
    //添加年卡
    @RequestMapping(value = "/addyear_card")
    public String addyear_card(Year_card year_card){
        year_card.setStar_regtime(new Date());
        year_card.setStatus("1");
        iVipService.addyear_card(year_card);
        System.out.println("year_card"+year_card);
        return "redirect:findIDR";
    }
    //通过id查询销售人员
    @RequestMapping(value = "/findIdSj" ,method = RequestMethod.GET)
    public String findIdSj(int pid,Model model){
        Pet_card idSj = iVipService.findIdSj(pid);
        model.addAttribute("idSj",idSj);
        return "pcard_Upgrade.jsp";
    }

    //查询大礼包详情
    int lid;
    @RequestMapping("/findOneSpree")
    @ResponseBody
    public List<Details> findOneSpree(int wid,HttpSession session){


        lid = wid;
        List<Details> list = iVipService.findOneSpree(wid);
        return list;
    }
    //添加大礼包
    @RequestMapping(value = "addGift")
    public String addGift(Gift gift){
        System.out.println("111"+lid);
        if (lid == 1 ){
            gift.setStar_regtime(new Date());
            gift.setGname("超级大礼包");
            gift.setPrice(1888);
        }else {
            gift.setPrice(888);
            gift.setGname("大礼包");
            gift.setStar_regtime(new Date());
        }
        iVipService.addGift(gift);
        return "redirect:findIDR";
    }
    //升级卡
    @RequestMapping(value = "updateSJPet_card")
    public String updateSJPet_card(Pet_card pet_card){
        iVipService.updateSJPet_card(pet_card);
        return "redirect:findIDR";
    }


}
